package zhoug.common.clip;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import zhoug.common.Extras;
import zhoug.logging.Logger;

/**
 * 自定义图片裁剪
 *
 * @Author: zhoug
 * @Date: 2022/9/6
 * @Description:
 */
public class ClipImageHelper extends BaseClipImageHelper {
    private static final String TAG = ">>>ClipImageHelper";

    /**
     * 选择框是否可以拖动
     */
    private boolean frameTransEnable = true;
    /**
     * 选择框是否可以缩放
     */
    private boolean frameScaleEnable = true;
    /**
     * 选择框默认宽高比
     */
    private float frameRatio = 0.75f;


    public static ClipImageHelper with(@NonNull ComponentActivity activity) {
        return new ClipImageHelper(activity);
    }

    public static ClipImageHelper with(@NonNull Fragment fragment) {
        return new ClipImageHelper(fragment);
    }


    protected ClipImageHelper(ComponentActivity activity) {
        super(activity);
    }

    protected ClipImageHelper(Fragment fragment) {
        super(fragment);
    }


    public ClipImageHelper setFrameTransEnable(boolean frameTransEnable) {
        this.frameTransEnable = frameTransEnable;
        return this;
    }

    public ClipImageHelper setFrameScaleEnable(boolean frameScaleEnable) {
        this.frameScaleEnable = frameScaleEnable;
        return this;
    }

    public ClipImageHelper setFrameRatio(float frameRatio) {
        this.frameRatio = frameRatio;
        return this;
    }


    @Override
    public ClipImageHelper setContext(Context context) {
        super.setContext(context);
        return this;
    }

    /**
     * 设置需要裁剪的图片
     *
     * @param inputData
     * @return
     */
    @Override
    public ClipImageHelper setInputData(Uri inputData) {
        super.setInputData(inputData);
        return this;
    }

    /**
     * 设置裁剪后输出的图片
     *
     * @param outputData
     * @return
     */
    @Override
    public ClipImageHelper setOutputData(Uri outputData) {
        super.setOutputData(outputData);
        return this;
    }

    /**
     * 设置裁剪后输出的图片 的格式
     *
     * @param outputFormat
     * @return
     */
    @Override
    public ClipImageHelper setOutputFormat(Bitmap.CompressFormat outputFormat) {
        super.setOutputFormat(outputFormat);
        return this;
    }

    /**
     * 设置裁剪监听
     *
     * @param onImageClipResultListener
     * @return
     */
    @Override
    public ClipImageHelper setOnImageClipResultListener(OnImageClipResultListener onImageClipResultListener) {
        super.setOnImageClipResultListener(onImageClipResultListener);
        return this;
    }

    @Override
    public boolean start() {
        if (inputData == null) {
            Logger.e(TAG, "inputData 不能为null");
            return false;
        }
        if (outputData == null) {
            Logger.e(TAG, "outputData 不能为null");
            return false;
        }
        if (context == null) {
            Logger.e(TAG, "context 不能为null");
            return false;
        }
        Logger.d(TAG, "inputData:" + inputData);
        Logger.d(TAG, "outputData:" + outputData);

        Intent intent = new Intent(context, ClipImageActivity.class);
        intent.putExtra(Extras.Clip.EXTRA_INPUT_DATA, inputData);
        intent.putExtra(Extras.Clip.EXTRA_OUTPUT_DATA, outputData);
        intent.putExtra(Extras.Clip.EXTRA_OUTPUT_FORMAT, outputFormat != null ? outputFormat.toString() : null);
        intent.putExtra(Extras.Clip.EXTRA_FRAME_TRANS_ENABLE, frameTransEnable);
        intent.putExtra(Extras.Clip.EXTRA_FRAME_SCALE_ENABLE, frameScaleEnable);
        intent.putExtra(Extras.Clip.EXTRA_FRAME_RATIO, frameRatio);
        mLauncher.launch(intent);
        return true;
    }


}
